#ifndef UHF_RFID_READER_H
#define UHF_RFID_READER_H

#include <QMainWindow>
#include <QButtonGroup>
#include <QtNetwork>
#include <QDebug>
#include <QCoreApplication>
#include <QCloseEvent>
#include <QList>
#include <list>
#include <map>
#include <string>
#include <cstring>
#include <stdlib.h>
#include <QStringList>
#include "ReaderMethod.h"
#include "DealMessage.h"
//#include "CompareMsgThread.h"
#include "AnalysisMsg.h"
#include <QMutex>
//QList<MapEpcData> epcData_list;
extern unsigned char g_btReadId;
extern QMutex g_mutex;
extern list<string> g_msgList;

namespace Ui {
class UHF_RFID_Reader;
}

class UHF_RFID_Reader : public QMainWindow
{
    Q_OBJECT

public:
    explicit UHF_RFID_Reader(QWidget *parent = 0);
    ~UHF_RFID_Reader();


    void closeEvent( QCloseEvent * event );


private slots:
    //自定义槽函数
    void OnConnected_Tcp();
    void ReadMessage();
    void UnlockInterface();
    void StopInventory();

    //界面刷新槽函数
    void ShowLineEditVersion(char* version, int len);
    void ShowLineEditTemp(char* pTemp, int len);
    void ShowLineEditIdentifier(QString identifier);
    void ShowLineEditOutputPower(int outputPower);
    void ShowInventory(InventoryCount inventoryData);
    void ShowReadTagData(TagReadBack tagData);
    void ShowEpcBuffer(EpcData epcData);
    void ShowFastInventory(int labeltime, int commandtime);
   // void ShowReadTagData1(ReadTagData tagData);
    void ShowFastEpcBuffer(EpcData epcData);
    void DrmModeGet(unsigned char drmMode);
    void ReadWorkAnt(unsigned char workAnt);
    void AntDetector(unsigned char antState);
    void ReturnLoss(unsigned char returnLoss);
    void FreqRegion(FreqRegionDefault freq);
    void CustomFreqRegion(FreqRegionCustom freq, int startFreq);
    void RfLinkProfile(unsigned char profileID);
    void GetAccessEpc(unsigned char* epc, int epcLen);
    void ShowGPIOValue(unsigned char gpio1, unsigned char gpio2);

    void ShowTxtLog(string strlog);

    //界面按钮槽函数
    void On_Connect_Radios_Clicked(int id);
    void On_Drm_Radios_Clicked(int id);
    void On_Freq_Radios_Clicked(int id);
    void On_RfLink_Radios_Clicked(int id);
    void On_ReadTag_Radios_Clicked(int id);
    void On_Beeper_Radios_Clicked(int id);
    void On_lockMembank_Radios_Clicked(int id);
    void On_locktype_Radios_Clicked(int id);
    void On_gpio1_Radios_Clicked(int id);
    void On_gpio2_Radios_Clicked(int id);
    void On_gpio3_Radios_Clicked(int id);
    void On_gpio4_Radios_Clicked(int id);

    void on_btnConnect_Tcp_clicked();
    void on_btnDisconnect_Tcp_clicked();
    void on_btn_ReadTemp_clicked();
    void on_btnReset_clicked();
    void on_btn_ReadVersion_clicked();
    void on_btnReadIdentifier_clicked();
    void on_btn_GetOutputPower_clicked();
    void on_btnGetDrmMode_clicked();
    void on_btnSetDrmMode_clicked();
    void on_btn_SetOutputPower_clicked();
    void on_ckBox_Session_clicked();
    void on_btnInventory_clicked();
    void on_ckBox_Ant1_clicked();
    void on_ckBox_Ant2_clicked();
    void on_ckBox_Ant3_clicked();
    void on_ckBox_Ant4_clicked();
    void on_radioButton_Beeper1_clicked();
    void on_btnGetWorkAnt_clicked();
    void on_btnFastInventory_clicked();
    void on_btnStopInventory_clicked();
    void on_btnStopFastInventory_clicked();
    void on_btnSetWorkAnt_clicked();
    void on_btnReturnLoss_clicked();
    void on_btnAntDetectorGet_clicked();
    void on_btnAntDetectorSet_clicked();
    void on_btnGetFreqRegion_clicked();
    void on_ckBox_Custom_clicked();
    void on_btnSetFreqRegion_clicked();
    void on_btnGetLinkPro_clicked();
    void on_btnSetLinkPro_clicked();
    void on_btnChooseTag_clicked();
    void on_btnReadTag_clicked();
    void on_btnReadSet_clicked();
    void on_btnBuzzerSet_clicked();
    void on_btnWriteLalel_clicked();
    void on_btnRefresh_clicked();
    void on_pushButton_26_clicked();
    void on_btnDestoryLabel_clicked();
    void on_readGpio_clicked();
    void on_setGpio3_clicked();
    void on_setGpio4_clicked();

    void on_btnTest_clicked();
    void On_tTcpSocket_connected();
    void On_tTcpSocket_readyRead();

private:
    void InitMapRssi();
    void InitFreqPoint();
    // 界面相关初始化
    void InitServer();
    void InitButton();
    void InitRadio();
    void InitWidget();
    void timerEvent(QTimerEvent *);
    void LockInterface();

    //自定义处理函数
    void CopyCommArg();
    void WaitSec(int sec);
    int  findEpcWidget(string strepc, int& num, int flag);
    void GetFastAnt(AntData *antData, int nLen);
    char a2x(char ch);
    int  HexToChar(char* inbuffer, int iLen, char* outbuffer, int& oLen);
    void cutStrZero(string& str);
    void compareMsg();

signals:
    void closeWindow();

private:
    Ui::UHF_RFID_Reader *ui;
    QButtonGroup* m_conncet_radios;
    QButtonGroup* m_drm_radios;
    QButtonGroup* m_freqregion_radios;
    QButtonGroup* m_linkprofile_radios;
    QButtonGroup* m_readtag_radios;
    QButtonGroup* m_beeper_radios;
    QButtonGroup* m_lockMembank_radios;
    QButtonGroup* m_locktype_radios;

    QButtonGroup* m_gpio1_radios;
    QButtonGroup* m_gpio2_radios;
    QButtonGroup* m_gpio3_radios;
    QButtonGroup* m_gpio4_radios;

    //CompareMsgThread* m_compareMsgThread;
    AnalysisMsg*   m_alysisMsg;

    QTcpSocket* m_TcpSocket;
    unsigned char freqregion;

    list<string> m_msgList;


    //已盘存标签
    QStringList m_labelList;

    //网络连接变量
    int m_Connect;
    int m_time0;

    ReaderMethod*   m_readerMethod;
    DealMessage*    c_pDealData;

    CommArg         m_commArg;
    QList<MapEpcData> list_epcData;
    map<unsigned char, string> m_mapRssi;
    map<unsigned char, string> m_mapFreq;


    int m_logNum;

    //盘存键是否按下
    bool m_isInventory;
    bool m_isInventoryFast;
    //解析数据操作是否完成
    bool m_dealIsOver;

    //盘存操作前，需要先设置工作天线，用于标识当前是否在执行盘存操作
    bool m_Inventory;
    //标识是否统计命令执行时间，当前仅盘存命令需要统计实际
    bool m_bReckonTime;
    //实时盘存锁定操作
    bool m_bLockTab;
    //ISO18000标签连续盘存标识
    bool m_bContinue;

    //记录ISO18000标签循环写入次数
    int m_nLoopTimes;
    //记录ISO18000标签写入字符数
    int m_nBytes;
    //记录ISO18000标签已经循环写入次数
    int m_nLoopedTimes;
    //实时盘存次数
    int m_nTotal;
    //列表更新频率
    int m_nRealRate;
    //记录快速轮询天线参数
    char m_btAryData[10];
    //记录快速轮询总次数
    int m_nSwitchTotal;
    int m_nSwitchTime;
    int m_nReceiveFlag;

    //设置工作天线
    int m_nWorkAnt;
    //已读取标签数量
    int m_nTagNum1;
    int m_nTagNum2;
    int m_nTagNum3;

    //总读取标签次数
    int m_nTotalRead1;
    int m_nTotalRead2;

    //快速盘存标记
    bool m_bFastInventory;

    //累计返回的数据条数
    int m_nBackNum1;
    int m_nBackNum2;
    //累计时间
    double m_lTotalTime1;
    double m_lTotalTime2;

    //DRM状态
    unsigned char m_btDrmMode;

    //射频通信链路
    unsigned char m_profileId;

    unsigned char m_memBank;
    unsigned char m_memBankLock;

    unsigned char m_lockType;

    unsigned char m_beeper;

    bool m_btcpInit;


    QTcpSocket* t_TcpSocket;  //tcp套接字
    bool t_connected;         //已连接

};

#endif // UHF_RFID_READER_H

















